package com.spring.cloud.examples.controller;

import com.spring.cloud.examples.es.entity.Product;
import com.spring.cloud.examples.es.service.ProductESService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/product/es")
public class ProductESController {

    @Autowired
    private ProductESService productESService;

    @RequestMapping("/createIndex")
    @ResponseBody
    public String createIndex()
    {
        Product product = new Product();
        product.setName("小米手机1");
        product.setBrands("小米");
        product.setCategory("手机");
        productESService.save(product);

        product = new Product();
        product.setName("小米手机2");
        product.setBrands("小米");
        product.setCategory("手机");
        productESService.save(product);

        product = new Product();
        product.setName("苹果手机1");
        product.setBrands("苹果");
        product.setCategory("手机");
        productESService.save(product);

        product = new Product();
        product.setName("苹果手机2");
        product.setBrands("苹果");
        product.setCategory("手机");
        productESService.save(product);

        product = new Product();
        product.setName("小米苹果手机");
        product.setBrands("小米苹果");
        product.setCategory("手机");
        productESService.save(product);

        return "注册成功";
    }


    /**
     * 只查询第一页
     * @param keyword
     * @return
     */
    @RequestMapping("/keyword")
    @ResponseBody
    public List<Product> queryByKeyword(String keyword)
    {
        return productESService.queryByKeyword(keyword);
    }

}
